<template>
  <div class="consultation">
    <h2>角色管理</h2>
    <h5>系统默认角色：管理员、会员、普通用户 无法修改</h5>
    <el-button type="primary" size="mini" @click="addData">新增</el-button>
    <div class="consultation-block">
      <div class="table">
        <el-table size="mini" :data="tableData" style="width: 100%">
          <el-table-column label="序号" type="index"> </el-table-column>
          <el-table-column prop="roleName" label="角色名称"> </el-table-column>
          <el-table-column label="操作" width="250">
            <template slot-scope="scope">
              <el-button
                type="warning"
                v-if="!scope.row.default"
                size="mini"
                @click="updateData(scope.row)"
                >修改</el-button
              >
              <el-button
                type="danger"
                v-if="!scope.row.default"
                size="mini"
                @click="deleteData(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    
    <el-dialog
      :title="dialogTitle"
      :visible.sync="isVisible"
      width="50%"
      :before-close="handleClose"
    >
      <el-form
        :model="form"
        status-icon
        :rules="rules"
        ref="form"
        label-width="90px"
        class="demo-form"
      >
        <el-form-item label="名称" prop="roleName">
          <el-input type="text" v-model="form.roleName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getRole, addRole, deleteRole, updateRole } from "../service.js";
export default {
  name: "home",
  mounted() {
    this.refreshTable();
  },
  data() {
    return {
      tableData: [],
      roleDefault: [
        {
          roleName: '管理员',
          default: true
        },
        {
          roleName: '会员',
          default: true
        },
        {
          roleName: '普通用户',
          default: true
        }
      ],
      isVisible: false,
      dialogTitle: "",
      form: {
        roleName: "",
      },
      rules: {
        roleName: [{ required: true, message: "请输入", trigger: "blur" }],
      },
    };
  },
  methods: {
    handleClose() {
      this.isVisible = false;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (!this.form._id) {
            addRole(this.form).then((res) => {
              if (res.code === 200) {
                this.$message.success("新增成功");
                this.isVisible = false;
                this.refreshTable();
              }
            });
            return;
          }
          updateRole(this.form._id, this.form).then((res) => {
            if (res.code === 200) {
              this.$message.success("编辑成功");
              this.refreshTable();
              this.isVisible = false;
            }
          });
        } else {
          return false;
        }
      });
    },
    addData() {
      this.dialogTitle = "新增";
      this.isVisible = true;
      this.$nextTick(() => {
        this.$refs.form.resetFields()
      })
    },
    refreshTable() {
      getRole().then((res) => {
        this.tableData = res.code === 200 ? this.roleDefault.concat(res.data) : this.roleDefault;
      });
    },
    updateData(row) {
      this.form = {
        ...row
      };
      this.dialogTitle = "修改";
      this.isVisible = true;
    },
    deleteData(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteRole(row._id).then((res) => {
            if (res.code === 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.refreshTable();
            }
          });
        })
        .catch(() => {
          
        });
    },
  },
};
</script>

<style scoped>
h2 {
  text-align: center;
  margin-top: 20px;
  margin-bottom: 20px;
}
h5 {
  color: #918787;
  margin-bottom: 30px;
}
.consultation-block {
  width: 90%;
  margin-left: 5%;
}

.pagination {
  text-align: center;
  margin-top: 20px;
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}

.el-icon-circle-close {
  position: absolute;
  top: -5px;
  right: -5px;
  cursor: pointer;
}
</style>
